// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Air Bubble Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 43 : TYPE_AIRBUBBLE


// Static Values

// Tables

event ObjectMain
	currentPlayer = object.value2
	if object.state == 0
		object.value0++
		if object.value0 > 15
			object.value0 = 0
			if object.frame == 8
				object.type = TypeName[Blank Object]
			end if
			if object.frame < object.propertyValue
				object.frame++
			end if
		end if
		object.ypos += object.yvel
		if currentPlayer != 0xFFFF
			if object[currentPlayer].state == PlayerObject_Clinging
				if object.propertyValue < 3
					object.value1 += 0x40000
				end if
			end if
		end if
		if object.frame < 7
			Sin(object.xpos, object.angle)
			object.xpos <<= 9
			object.xpos += object.value1
			object.angle += 4
			object.angle &= 511
		end if
		if object.iypos < stage.waterLevel
			if object.propertyValue == 6
				object.frame = 7
				object.propertyValue = 8
				object.value0 = 0
				object.yvel = 0
			else
				if object.propertyValue < 6
					object.type = TypeName[Blank Object]
				end if
			end if
		end if
	else
		CheckEqual(object[currentPlayer].animation, ANI_HURT)
		temp0 = checkResult
		CheckEqual(object[currentPlayer].animation, ANI_DYING)
		temp0 |= checkResult
		if temp0 != 0
			object.value3 = 0
		end if
		if object.value0 < 20
			object.value0++
			if object.value3 != 0
				object[currentPlayer].animation = ANI_BREATHING
				object[currentPlayer].rotation = 0
				object[currentPlayer].value24 = 0
			end if
			if object.value0 > 9
				object.frame = 8
			end if
		else
			object.type = TypeName[Blank Object]
			if object.value3 != 0
				object[currentPlayer].animation = ANI_WALKING
				object[currentPlayer].animationSpeed = 20
			end if
		end if
	end if
	if object.outOfBounds == 1
		object.type = TypeName[Blank Object]
	end if
	if object.frame == 6
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0			
				if object[currentPlayer].animation != ANI_DYING
					if object[currentPlayer].value37 != 2
						BoxCollisionTest(C_TOUCH, object.entityPos, -2, -2, 2, 2, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							object.frame = 7
							object.state = 1
							object.value0 = 0
							object.yvel = 0
							object.value2 = currentPlayer
							object[currentPlayer].yvel = 0
							object[currentPlayer].xvel = 0
							object[currentPlayer].speed = 0
							CheckEqual(object[currentPlayer].animation, ANI_FLYING)
							temp0 = checkResult
							CheckEqual(object[currentPlayer].animation, ANI_FLYINGTIRED)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_SWIM_LIFT)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_GLIDING)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_GLIDING_STOP)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_CLIMBING)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_LEDGEPULLUP)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_CLINGING)
							temp0 |= checkResult
							CheckEqual(object[currentPlayer].animation, ANI_FANROTATE)
							temp0 |= checkResult
							if temp0 == 0
								object[currentPlayer].animation = ANI_BREATHING
								object.value3 = 1
							end if
							object[currentPlayer].value3 = 0
							object[currentPlayer].value4 = 0
							CallFunction(Water_Function109)
							if object[currentPlayer].state == PlayerObject_RollingJump
								object[currentPlayer].state = PlayerObject_HandleAir
							end if
							if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
								object[currentPlayer].state = PlayerObject_HandleAir
							end if
							if object[currentPlayer].state == PlayerObject_FlyCarry
								object[currentPlayer].state = PlayerObject_HandleAir
								object[1].value36 = 30
							end if
							PlaySfx(SfxName[Breathing], 0)
						end if
					end if
				end if
			end if	
		next
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-2, -2, 4, 4, 61, 1)
	SpriteFrame(-3, -3, 6, 6, 61, 6)
	SpriteFrame(-4, -4, 8, 8, 68, 4)
	SpriteFrame(-6, -6, 12, 12, 77, 0)
	SpriteFrame(-8, -8, 16, 16, 90, 1)
	SpriteFrame(-12, -12, 24, 24, 61, 13)
	SpriteFrame(-16, -16, 32, 32, 86, 18)
	SpriteFrame(-16, -16, 32, 32, 123, 1)
	SpriteFrame(-15, -15, 30, 30, 156, 3)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
